<template>
  <div class="dec-btn"
      :class="timeLeft !== 0 ? 'disabled-btn' : ''"
      @click="clickBtn">{{ timeLeft ? timeLeft + 'S': text}}</div>
</template>
<script>
export default {
  data () {
    return {
      timeLeft: 0,
      timer: null
    }
  },
  props: {
    text: {
      type: String,
      default: ''
    },
    time: {
      type: Number,
      default: 1
    }
  },
  methods: {
    timeDec (number) {
      this.timeLeft = number - 1
      this.timer = setTimeout(() => {
        if (this.timeLeft === 0) {
          clearTimeout(this.timer)
          this.timer = null
          return
        }
        this.timeDec(this.timeLeft)
      }, 1000)
    },
    clickBtn () {
      if (this.timeLeft) return
      this.$emit('trigger')
      this.timeDec(this.time)
    }
  }
}
</script>
<style lang="less" scoped>
  .dec-btn {
    user-select: none;
    float: right;
    height: 20px;
    line-height: 20px;
    margin-top: 8px;
    margin-right: 10px;
    padding: 0 15px;
    font-size: 12px;
    border-radius: 10px;
    border: 1px solid #FFF;
    background: #FFF;
    color: #000;
    cursor: pointer;
  }
  .dec-btn:hover {
    background: #569cd6;
    border: 1px solid #569cd6;
    color: #FFF;
  }
  .disabled-btn {
    background: #DDD;
    border: 1px solid #DDD;
    color: #666;
  }
</style>
